/*
 * Copyright (c) 2013.
 * 游戏服务器核心代码编写人陈磊拥有使用权
 * 联系方式：E-mail:13638363871@163.com ;qq:502959937
 * 个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver;


import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.extComponents.nettyEngine4.ServerHandler;
import com.dc.gameserver.extComponents.nettyEngine4.ServerServiceImpl;
import com.dc.gameserver.extComponents.springJredisCache.JRedisCache;
import com.dc.gameserver.serverCore.model.dbsql.mapper.TUserMapper;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.dc.gameserver.serverCore.service.managerService.ThreadPoolManager;
import org.apache.log4j.xml.DOMConfigurator;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :陈磊 <br/>
 *         Date: 13-3-15
 *         Time: 下午11:31
 *         connectMethod:13638363871@163.com<br/>
 *服务器启动入口，初始化服务器所需要的配置，负责调用需要的启动的对象。
 * 详细信息请看readme.properties
 * 注意：用spring管理bean，那么bean必须由spring容器产生，否则会注入bean失败
 *
 */
@SuppressWarnings("unchecked")
@Service("gameServer")
public class launcher {

    private static final Logger LOG = LoggerFactory.getLogger(launcher.class);

    @Resource
    private ModelCacheService modelCacheService;

    @Resource
    private ServerServiceImpl serverService;

    @Resource
    private TUserMapper tUserMapper;
//
    @Resource
    private SqlSessionTemplate sqlSessionTemplate;

    @Resource
    private PlayerService playerService;
    @Resource
    private JRedisCache jRedisCache;





    public static void main(String[] args) {
        try {

            DOMConfigurator.configure(Config.DEFAULT_VALUE.FILE_PATH.LOG4J);

            System.setProperty("java.net.preferIPv4Stack", "true"); //Disable IPv6 in JVM
            /**初始化spring容器*/
            BeanFactory springContext = new FileSystemXmlApplicationContext(Config.DEFAULT_VALUE.FILE_PATH.SPRING_CONFIG_PATH);
            launcher gameServer = (launcher) springContext.getBean("gameServer");
            /**set  spring context**/
            ServerHandler.setSpringContext(springContext);

            /**初始化配置文件，启动通信层*/
            gameServer.serverService.IntiServer();
             /**初始化cardBox容器*/
            CardBoxManager.IntiCardBoxList();
             /***初始化背包系统对象池***/
            PlayerInstance.IntiItemCellPool();
            /**初始化线程池管理类**/
            ThreadPoolManager.getInstance();
            /**加载游戏脚本资源文件*/
            ScriptFactory.getInstance();

            /**cache db roleName**/
            gameServer.playerService.IntiRoleNameIntoCache();


            gameServer.modelCacheService.findList(Config.DEFAULT_VALUE.SERVER_VALUE.gameserverPort);


//            System.out.print(GameFormula.getCardType());
//            System.out.print(",");

//            ArrayList<Integer> list= new ArrayList<Integer>();
//            list.add(1);
//            list.add(2);
//            list.add(3);
//            gameServer.jRedisCache.putList("1",list);
//            ArrayList<Integer>  cacheList= (ArrayList<Integer>) gameServer.jRedisCache.getList("1");
//            System.out.println(cacheList);

            /**test object
             * 针对object  修改 替换的是key对应的属性，不是pop
             * **/
//            TUser tUser=new TUser();
//            tUser.setUseracount("12345");
//            gameServer.modelCacheService.addObject(1,tUser);
//            TUser otherUser1= (TUser) gameServer.modelCacheService.find(1);
//            //is pop??  object not pop
//            TUser otherUser2= (TUser) gameServer.modelCacheService.find(1);
//             //change attr
//            otherUser1.setUseracount("石头哥哥");
//            gameServer.modelCacheService.addObject(1,otherUser1);
//            TUser otherUser3= (TUser) gameServer.modelCacheService.find(1);
//            System.out.println();






            /**test unit**/
//            MonsterModel monsterModel=ScriptFactory.monsterModelFastMap
//                    .get(ScriptFactory.pointModelFastMap.get(1).getEnemyID());
//            Monster monster=new Monster(monsterModel);
//            PlayerInstance player=new PlayerInstance();
//            CardBox cardBox=CardBoxManager.getNewCardBox();
//            int cardBoxID=cardBox.getCardBoxID();
//            monster.setCardBoxID(cardBoxID);
//            player.setCardBoxID(cardBoxID);
//            cardBox.getInGamePlayerList().addAll(player,monster);
//            cardBox.ClearAndIntiCardBox(player, monster);
//            FastTable<Card> monsterCards=monster.monsterOutCards();
//            System.out.println(monsterCards);

//            TUserExample tUserExample=new TUserExample();

            //test drop item
//            PointModel levelModel=ScriptFactory.pointModelFastMap.get(1); //关卡模型
//            /**
//             * 初始化玩家战斗属性  ，怪物战斗属性
//             */
//            BaseBattle playerNpcBattle=
//                    new PlayerMonsterBattle(null,
//                            new Monster(ScriptFactory.monsterModelFastMap
//                                    .get(levelModel.getEnemyID())),null,levelModel);
//
//            int[]ids=playerNpcBattle.invokeItem();



            /***begin + 5 (length)**/
//            Page page=new Page(0,5);    //数据库开始的位置
//            page.setCount(gameServer.tUserMapper.countByExample(tUserExample));
//            tUserExample.setLimitStart(page.getBegin());
//            tUserExample.setLimitEnd(page.getEnd());
//            List<TUser> userList= gameServer.tUserMapper.selectByExample(tUserExample);
//            System.out.println(userList);
//            page.setBegin(5);
//            tUserExample.setLimitStart(page.getBegin());
//            tUserExample.setLimitEnd(page.getEnd());
//            List<TUser> userList2= gameServer.tUserMapper.selectByExample(tUserExample);
//            System.out.println(userList2);
//
//            page.setBegin(10);
//            tUserExample.setLimitStart(page.getBegin());
//            tUserExample.setLimitEnd(page.getEnd());
//            List<TUser> userList3= gameServer.tUserMapper.selectByExample(tUserExample);
//            System.out.println(userList3);



            //刷入帐号
//            TUser tUser=new TUser();
//            SqlSession sqlSession=gameServer.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH,false);
//            TUserMapper userMapper=sqlSession.getMapper(TUserMapper.class);
//            for (int i=0;i!=1000;++i){
//                tUser.setUseracount("dc"+i);
//                tUser.setUserpwd("123");
//                userMapper.insert(tUser);
//            }
//            long start=System.currentTimeMillis();
//            sqlSession.commit();
//            sqlSession.clearCache();
//            sqlSession.close();
//            System.out.println(System.currentTimeMillis()-start+" millis");

//            List<TUser> roleList=gameServer.tUserMapper.selectByExample(new TUserExample());



            /**test object***/
//            TUser user=new TUser();
//            user.setUseracount(" 石头哥哥");
//            gameServer.modelCacheService.addObject(1,user);
//            TUser tUser= (TUser) gameServer.modelCacheService.find(1);
//            System.out.println(tUser.getUseracount());
//            tUser.setUseracount("123");
//            gameServer.modelCacheService.addObject(1,tUser);
//            TUser tUser2= (TUser) gameServer.modelCacheService.find(1);
//            System.out.println(tUser2.getUseracount());
////

            /***
             * test list add a arrayList ,get  make a FastTable recive data
             * can not  cast  to the FastTable,because the obj is arrayList!
             * **/
//            FastTable<String> integerFastTable=new FastTable<String>();
//            for (int i=0;i!=3;++i){
//                integerFastTable.add(i+"");
//            }
//            gameServer.modelCacheService.addList(1, integerFastTable);
//            FastTable<String> integers= (FastTable<String>)gameServer.modelCacheService.findList(1);
//            integers.remove(1+"");
//            gameServer.modelCacheService.addList(1,integers);
//            FastTable<String> integersd= (FastTable<String>)gameServer.modelCacheService.findList(1);
//            FastTable<String> we= (FastTable<String>)gameServer.modelCacheService.findList(1);
//            System.out.println("from redis data:"+integersd);


//            FastTable<TUser> integerFastTable=new FastTable<TUser>();
//            TUser tUser=new TUser();
//            tUser.setUseracount("123456");
//            integerFastTable.addFirst(tUser);
//            gameServer.modelCacheService.addList(1,integerFastTable);
//
//            integerFastTable= (FastTable<TUser>) gameServer.modelCacheService.find(1);
//            tUser=integerFastTable.getFirst();
//            tUser.setUseracount("石头哥哥");
//            integerFastTable.addFirst(tUser);
//            System.out.println("from redis data:");






            //修改数据
//            for (int i=0;i!=Short.MAX_VALUE-1;++i){
//                integers.remove(i+"");
//            }
//             //return cache
//            gameServer.modelCacheService.addList(1,integers);
//            //get data
//            FastTable<String> integersd= (FastTable<String>)gameServer.modelCacheService.findList(1);
//            for (String i:integersd){
//                System.out.println("from redis data:"+i);
//            }

         gameServer.serverService.run();

        } catch (Exception e) {
            LOG.error("server start error", e);
        }

    }


}
